package com.example.azheng.okhttpdemo.presenter;


import android.arch.lifecycle.LifecycleOwner;
import android.util.Log;

import com.example.azheng.okhttpdemo.base.BasePresenter;
import com.example.azheng.okhttpdemo.beans.JuheBean;
import com.example.azheng.okhttpdemo.contract.JuheNewContract;
import com.example.azheng.okhttpdemo.model.JuheNewModel;
import com.example.azheng.okhttpdemo.retrofit.RxScheduler;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;


import io.reactivex.Observer;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author azheng
 * @date 2018/4/14.
 * GitHub：https://github.com/RookieExaminer
 * email：wei.azheng@foxmail.com
 * description：
 */
public class JuheNewPresenter extends BasePresenter<JuheNewContract.View> implements JuheNewContract.Presenter {
    private JuheNewContract.Model mModel;


    public JuheNewPresenter() {
        this.mModel = new JuheNewModel();
    }

    @Override
    public void onRefresh() {
        if (!isViewAttached()) {
            //如果没有View引用就不加载数据

            return;
        }
        mView.showLoading();
        mModel.request()
                .compose(RxScheduler.<JuheBean>Obs_io_main())

                .as(mView.<JuheBean>bindAutoDispose())
                .subscribe(new Observer<JuheBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        //向观察员提供取消（处置）的手段

                    }

                    @Override
                    public void onNext(JuheBean juheBean) {
                        //调用view接口显示数据
                        if (isViewAttached()) {
                            if (null != juheBean) {
                                if (0 == juheBean.getError_code()) {
                                    mView.showData(juheBean);
                                }
                            } else {

                            }
                            mView.hideLoading();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        //调用view接口提示失败信息
                        if (isViewAttached()) {
                            mView.getDataFailed();
                            mView.hideLoading();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //所有事件都完成，可以做些操作
                    }
                });
    }


}
